package xyz.xtt.asset.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.github.yulichang.toolkit.JoinWrappers;
import com.github.yulichang.wrapper.MPJLambdaWrapper;

import xyz.xtt.asset.api.enums.StatusAllEnum;
import xyz.xtt.asset.client.response.NftListVo;
import xyz.xtt.asset.entity.TblAsset;
import xyz.xtt.asset.entity.TblAssetAccount;
import xyz.xtt.asset.entity.TblAssetExt;
import xyz.xtt.asset.entity.TblAssetStaticResource;
import xyz.xtt.asset.enums.UpChainEnum;
import xyz.xtt.asset.mapper.TblAssetAccountMapper;
import xyz.xtt.asset.mapper.TblAssetMapper;
import xyz.xtt.asset.service.IMyService;
import xyz.xtt.asset.service.ITblCollectionService;
import xyz.xtt.asset.vo.CollectionVo;

/**
 * @author huqibo
 * @date 2025/03/24
 */
@Service
public class MyService implements IMyService {
	@Autowired
	private TblAssetAccountMapper tblAssetAccountMapper;
	@Autowired
	private TblAssetMapper tblAssetMapper;
	@Autowired
	private ITblCollectionService iTblCollectionService;
	@Override
	public List<NftListVo> getNftListByUidV2(String uid, Integer currPage, Integer pageSize, String queryType, int businessType, int orderType) {
		Long lUid = Long.parseLong(uid);
		if (Objects.isNull(queryType)) {
			return null;
		}
		List<NftListVo> list = new ArrayList<>();
		switch (queryType) {
		case "0" -> list = getOwnerNftList(uid, currPage, pageSize, businessType, orderType, lUid);
		case "1" -> list = getMyNftList(uid, currPage, pageSize, businessType, orderType, UpChainEnum.YES);
		case "3" -> list = getMyNftList(uid, currPage, pageSize, businessType, orderType, UpChainEnum.NO);
		}
		if (Objects.isNull(list) || list.isEmpty()) {
			return null;
		}
		// 获取风格列表
		List<CollectionVo> styleList = iTblCollectionService.getListAll();
		Map<Long, CollectionVo> map = styleList.stream().collect(Collectors.toMap(CollectionVo::getId, fruit -> fruit));
		// 处理风格
		list.forEach(x -> {
			// 处理风格
			CollectionVo collectionVo = map.get(x.getCollectionId());
			x.setCollectionImg(Objects.nonNull(collectionVo) && !collectionVo.getId().equals(1L) ? collectionVo.getImgUrl() : "");
		});
		return list;
	}

	private List<NftListVo> getOwnerNftList(String uid, Integer currPage, Integer pageSize, int businessType, int orderType, Long lUid) {
		MPJLambdaWrapper<TblAssetAccount> query = JoinWrappers
				.lambda(TblAssetAccount.class)
				.select(TblAsset::getId, TblAsset::getAssetName, TblAsset::getDescription, TblAsset::getGoodsId, TblAsset::getCollectionId)
				.select(TblAsset::getUnit, TblAsset::getFirstType, TblAsset::getSecondType, TblAsset::getAmount, TblAsset::getPopularityRate)
				.select(TblAssetAccount::getPrice)
				.selectAs(TblAssetAccount::getAmount, "ownerAmount")
				.selectAs(TblAssetAccount::getId, "assetAccountId")
				.select(TblAssetStaticResource::getLogoUri)
				.leftJoin(TblAsset.class, TblAsset::getId, TblAssetAccount::getAssetId)
				.leftJoin(TblAssetStaticResource.class, TblAssetStaticResource::getAssetId, TblAssetAccount::getAssetId)
				.leftJoin(TblAssetExt.class, TblAssetExt::getAssetId, TblAssetAccount::getAssetId)
				.ne(TblAsset::getAuthorId, uid)
				.eq(TblAssetAccount::getOwnerId, lUid)
				.eq(-1 < businessType, TblAsset::getFirstType, businessType)
				// 不显示不上链数据，
				.eq(TblAssetExt::getUpChain, UpChainEnum.YES.code())
				.gt(TblAssetAccount::getAmount, 0)
				.eq(TblAssetAccount::getVisiable, StatusAllEnum.YES.code())
				.orderBy(true, orderType != 0, TblAsset::getCreateDatetime)
				.last(getCurrentPage(currPage, pageSize));
		return tblAssetAccountMapper.selectJoinList(NftListVo.class, query);
	}

	// 我铸造的，不需要判断拥有数据
	private List<NftListVo> getMyNftList(String uid, Integer currPage, Integer pageSize, int businessType, int orderType, UpChainEnum upChainEnum) {
		MPJLambdaWrapper<TblAsset> wrapper = JoinWrappers
				.lambda(TblAsset.class)
				.select(TblAsset::getId, TblAsset::getAssetName, TblAsset::getDescription, TblAsset::getCollectionId, TblAsset::getGoodsId)
				.select(TblAsset::getUnit, TblAsset::getAmount, TblAsset::getFirstType, TblAsset::getSecondType, TblAsset::getPopularityRate)
				.select(TblAssetStaticResource::getLogoUri)
				.select(TblAssetAccount::getPrice)
				.selectAs(TblAssetAccount::getAmount, "ownerAmount")
				.selectAs(TblAssetAccount::getId, "assetAccountId")
				.leftJoin(TblAssetStaticResource.class, TblAssetStaticResource::getAssetId, TblAsset::getId)
				.leftJoin(TblAssetExt.class, TblAssetExt::getAssetId, TblAsset::getId)
				.leftJoin(TblAssetAccount.class, on -> on
						.eq(TblAssetAccount::getOwnerId, TblAsset::getAuthorId)
						.eq(TblAssetAccount::getAssetId, TblAsset::getId))
				.eq(TblAsset::getAuthorId, uid)
				.eq(TblAssetExt::getUpChain, upChainEnum.code())
				.eq(-1 < businessType, TblAsset::getFirstType, businessType)
				.eq(TblAssetAccount::getVisiable, StatusAllEnum.YES.code())
				.gt(TblAssetAccount::getAmount, 0)
				.orderBy(true, orderType != 0, TblAsset::getCreateDatetime);
		wrapper.last(getCurrentPage(currPage, pageSize));
		return tblAssetMapper.selectJoinList(NftListVo.class, wrapper);
	}

	private String getCurrentPage(Integer currPage, Integer pageSize) {
		return String.format("limit %d, %d", Math.max(((currPage - 1) * pageSize), 0), pageSize);
	}
}
